import { SkillBase } from '@/assets/skills/SkillBase.js'
import { GameObject } from '@/assets/GameObject.js'

export class Fireball extends SkillBase {
    constructor(player) {
        super(player, 0) // 3秒冷却时间
        this.player = player
        this.skillKey = 'Q'
        this.skillName = 'fireball'
        this.damage = 20
        this.speed = this.player.info.speed * 3
        this.range = this.player.gameMap.ctx.canvas.height
        this.radius = this.player.height * 0.01

        this.load_skill_icon('https://www.helloimg.com/i/2025/04/11/67f8dff24aa8d.jpg')
    }

    // 添加resize方法
    resize() {
        this.range = this.player.gameMap.ctx.canvas.height
        this.radius = this.player.height * 0.01
        this.speed = this.player.info.speed * 3
    }

    cast(tx, ty) {
        if (this.player.isDead || this.is_on_cooldown()) {
            return false
        }
        // 计算方向向量
        const angle = Math.atan2(ty - this.player.info.y, tx - this.player.info.x)
        const vx = Math.cos(angle)
        const vy = Math.sin(angle)
        // 创建火球实体
        this.create_fireball_entity(vx, vy)

        // 调用父类方法重置冷却
        super.cast()
        return true
    }

    create_fireball_entity(vx, vy) {
        const fireballInfo = {
            x: this.player.info.x,
            y: this.player.info.y,
            vx: vx,
            vy: vy,
            radius: this.radius,
            speed: this.speed,
            range: this.range,
            damage: this.damage,
            player: this.player,
        }
        new FireballEntity(this.gameMap, fireballInfo)
    }
}

// 火球粒子类
class FireballParticle {
    constructor(x, y, vx, vy, radius, lifespan, color) {
        this.x = x
        this.y = y
        this.vx = vx
        this.vy = vy
        this.radius = radius
        this.lifespan = lifespan
        this.maxLifespan = lifespan
        this.color = color
        this.alpha = 1.0
    }

    update(timeDelta) {
        // 更新位置
        this.x += this.vx * (timeDelta / 1000)
        this.y += this.vy * (timeDelta / 1000)

        // 缩小半径 - 加快收缩速度
        this.radius *= 0.9

        // 减少生命周期 - 增加衰减系数
        this.lifespan -= timeDelta * 1.5

        // 计算透明度
        this.alpha = this.lifespan / this.maxLifespan

        // 如果生命周期结束，返回false
        return this.lifespan > 0 && this.radius > 0.1
    }

    draw(ctx) {
        ctx.beginPath()
        ctx.globalAlpha = this.alpha

        // 创建径向渐变
        const gradient = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius)

        // 根据颜色设置渐变
        if (this.color === 'orange') {
            gradient.addColorStop(0, 'rgba(255, 255, 100, ' + this.alpha + ')')
            gradient.addColorStop(0.4, 'rgba(255, 120, 0, ' + this.alpha * 0.8 + ')')
            gradient.addColorStop(1, 'rgba(255, 0, 0, 0)')
        } else if (this.color === 'red') {
            gradient.addColorStop(0, 'rgba(255, 100, 100, ' + this.alpha + ')')
            gradient.addColorStop(0.4, 'rgba(255, 0, 0, ' + this.alpha * 0.8 + ')')
            gradient.addColorStop(1, 'rgba(100, 0, 0, 0)')
        }

        ctx.fillStyle = gradient
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
        ctx.fill()
        ctx.globalAlpha = 1.0
    }
}

// 火球实体类
class FireballEntity extends GameObject {
    constructor(gameMap, info) {
        super()
        this.gameMap = gameMap
        this.info = info
        this.eps = 0.1

        this.particles = []
        this.lastParticleTime = 0
        this.particleInterval = 15 // 生成粒子的时间间隔（毫秒）

        // 添加发光效果参数
        this.glowRadius = this.info.radius * 2.5
        this.glowAlpha = 0.4
        this.pulseSpeed = 0.005

        // 火球尾迹颜色
        this.trailColors = ['orange', 'red']
    }

    // 添加resize方法，更新火球参数以匹配窗口大小变化
    resize(widthRatio, heightRatio) {
        // 按比例缩放位置
        this.info.x *= widthRatio
        this.info.y *= heightRatio

        // 按比例缩放半径和其他属性
        this.info.radius *= heightRatio
        this.info.speed *= heightRatio
        this.info.range *= heightRatio
    }

    // 计算两点之间的距离
    get_dist(x1, y1, x2, y2) {
        return Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    }

    // 判断是否与玩家碰撞
    is_collision(player) {
        let distance = this.get_dist(player.info.x, player.info.y, this.info.x, this.info.y)
        return distance < this.info.radius + player.info.radius
    }

    // 生成粒子
    generate_particles() {
        const currentTime = Date.now()
        if (currentTime - this.lastParticleTime > this.particleInterval) {
            this.lastParticleTime = currentTime

            // 在火球后方生成粒子
            const offsetX = -this.info.vx * this.info.radius * 0.5
            const offsetY = -this.info.vy * this.info.radius * 0.5

            // 随机选择颜色
            const color = this.trailColors[Math.floor(Math.random() * this.trailColors.length)]

            // 随机粒子半径
            const radius = this.info.radius * (0.5 + Math.random() * 0.5)

            // 随机粒子速度（稍微偏离主方向）
            const particleSpeed = this.info.speed * 0.2
            const angle = Math.atan2(this.info.vy, this.info.vx) + (Math.random() - 0.5) * 0.8
            const vx = Math.cos(angle) * particleSpeed * (0.2 + Math.random() * 0.3)
            const vy = Math.sin(angle) * particleSpeed * (0.2 + Math.random() * 0.3)

            // 减少粒子生命周期
            const lifespan = 100 + Math.random() * 150

            // 创建并添加粒子
            this.particles.push(
                new FireballParticle(
                    this.info.x + offsetX,
                    this.info.y + offsetY,
                    -vx,
                    -vy,
                    radius,
                    lifespan,
                    color
                )
            )

            // 减少粒子最大数量限制
            if (this.particles.length > 30) {
                this.particles.shift()
            }
        }
    }

    // 更新粒子
    update_particles() {
        // 更新现有粒子
        for (let i = this.particles.length - 1; i >= 0; i--) {
            if (!this.particles[i].update(this.timeDelta)) {
                this.particles.splice(i, 1)
            }
        }
    }

    // 攻击玩家
    attack(player) {
        let angle = Math.atan2(player.info.y - this.info.y, player.info.x - this.info.x)
        player.be_attacked(angle, this.info.damage)

        this.destroy()
    }

    start() {}

    // 更新火球移动
    update_fireball_move() {
        // 检查火球是否已达到最大射程
        if (this.info.range < this.eps) {
            this.destroy()
            return false
        }
        // 计算本帧移动距离（取剩余射程和速度计算距离的较小值）
        let moved = Math.min(this.info.range, (this.info.speed * this.timeDelta) / 1000)
        // 更新火球位置（根据速度方向向量移动）
        this.info.x += this.info.vx * moved
        this.info.y += this.info.vy * moved
        // 减少剩余射程
        this.info.range -= moved
    }

    // 检查是否与玩家碰撞
    check_collision() {
        for (let i = 0; i < this.gameMap.players.length; i++) {
            let player = this.gameMap.players[i]
            // 检查玩家是否已死亡，以及是否是发射火球的玩家自己
            if (this.info.player !== player && !player.isDead && this.is_collision(player)) {
                this.attack(player)
            }
        }
    }

    update() {
        this.update_fireball_move()

        // 生成粒子
        this.generate_particles()

        // 更新粒子
        this.update_particles()

        this.check_collision()

        this.draw()
    }

    draw() {
        // 先绘制所有粒子
        for (let particle of this.particles) {
            particle.draw(this.gameMap.ctx)
        }

        // 绘制发光效果
        const pulseIntensity = Math.sin(Date.now() * this.pulseSpeed) * 0.3 + 0.7
        this.gameMap.ctx.beginPath()
        const gradient = this.gameMap.ctx.createRadialGradient(
            this.info.x,
            this.info.y,
            0,
            this.info.x,
            this.info.y,
            this.glowRadius * pulseIntensity
        )
        gradient.addColorStop(0, 'rgba(255, 200, 50, ' + this.glowAlpha * pulseIntensity + ')')
        gradient.addColorStop(
            0.5,
            'rgba(255, 120, 0, ' + this.glowAlpha * 0.5 * pulseIntensity + ')'
        )
        gradient.addColorStop(1, 'rgba(255, 0, 0, 0)')

        this.gameMap.ctx.fillStyle = gradient
        this.gameMap.ctx.arc(
            this.info.x,
            this.info.y,
            this.glowRadius * pulseIntensity,
            0,
            Math.PI * 2
        )
        this.gameMap.ctx.fill()

        // 绘制火球核心
        this.gameMap.ctx.beginPath()
        const coreGradient = this.gameMap.ctx.createRadialGradient(
            this.info.x,
            this.info.y,
            0,
            this.info.x,
            this.info.y,
            this.info.radius
        )
        coreGradient.addColorStop(0, 'rgba(255, 255, 200, 1)')
        coreGradient.addColorStop(0.3, 'rgba(255, 200, 50, 1)')
        coreGradient.addColorStop(0.7, 'rgba(255, 120, 0, 1)')
        coreGradient.addColorStop(1, 'rgba(255, 50, 0, 0.7)')

        this.gameMap.ctx.fillStyle = coreGradient
        this.gameMap.ctx.arc(this.info.x, this.info.y, this.info.radius, 0, Math.PI * 2)
        this.gameMap.ctx.fill()

        // 添加阴影效果
        this.gameMap.ctx.shadowBlur = 15
        this.gameMap.ctx.shadowColor = 'rgba(255, 100, 0, 0.8)'
        this.gameMap.ctx.fill()
        this.gameMap.ctx.shadowBlur = 0
    }
}
